<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>Constrain Draggable - jQuery EasyUI Demo</title>
		<link rel="stylesheet" type="text/css" href="../../../../js/lib/jquery-easyui-v1.5.1/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../../js/lib/jquery-easyui-v1.5.1/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../demo.css">
		<script type="text/javascript" src="../../../../js/lib/jquery-easyui-v1.5.1/jquery.min.js"></script>
		<script type="text/javascript" src="../../../../js/lib/jquery-easyui-v1.5.1/jquery.easyui.min.js"></script>
	</head>

	<body>
		<h2>Constrain Draggable</h2>
		<p>The draggable object can only be moved within its parent container.</p>
		<div style="margin:20px 0;"></div>
		<div id="p" class="easyui-panel" style="position:relative;overflow:hidden;width:500px;height:300px">
			<div class="easyui-draggable" data-options="onDrag:onDrag" style="width:100px;height:100px;background:#fafafa;border:1px solid #ccc;">
				<div id="c"></div>
			</div>
		</div>
		<script>
			function onDrag(e) {
				var d = e.data;
				$("#c").text('x:'+d.left+',y:'+d.top);
				//left
				if(d.left < 0) {
					d.left = 0;
					$('#p').css('border-left-color','red');
				}else{
					$('#p').css('border-left-color','#95B8E7');
				}
				//top
				if(d.top < 0) {
					d.top = 0;
					$('#p').css('border-top-color','red');
				}else{
					$('#p').css('border-top-color','#95B8E7');
				}
				//right
				if(d.left + $(d.target).outerWidth() > $(d.parent).width()) {
					d.left = $(d.parent).width() - $(d.target).outerWidth();
					$('#p').css('border-right-color','red');
				}else{
					$('#p').css('border-right-color','#95B8E7');
				}
				//bottom
				if(d.top + $(d.target).outerHeight() > $(d.parent).height()) {
					d.top = $(d.parent).height() - $(d.target).outerHeight();
					$('#p').css('border-bottom-color','red');
				}else{
					$('#p').css('border-bottom-color','#95B8E7');
				}
			}
		</script>

	</body>

</html>